package com.ymdx.util.responseSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.ResourceBundle;

/**
 * Created by yangliwei on 2017/8/13.
 */
public class ResponseTool implements Serializable {
    protected final Logger log = LoggerFactory.getLogger(getClass());
    private Integer status;
    private String code;
    private String message;
    private Object data;

    // 读取配置文件
    public static ResourceBundle resourceBundle;

    static {
        resourceBundle = ResourceBundle.getBundle("error_code");
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getCode() {
        return code;

    }

    public void setCode(String code) {
        this.code = code;
        try {
            message = new String(resourceBundle.getString(code).getBytes("ISO-8859-1"),"utf-8");
        } catch (Exception e) {
            message = "";
        }

    }


    //这两个方法很奇怪，不知道, 在返回结果时候可以获得message不知道反射的时候是否用到该方法
    public String getMessage() {
        return message;
    }

    private void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        //强力反射，让String类型的null变为""对性能有影响
//        BeanWrapper src = new BeanWrapperImpl(data);
//        PropertyDescriptor[] propertyDescriptors = src.getPropertyDescriptors();
//        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
//            String propName = propertyDescriptor.getName();
//            if (propertyDescriptor.getReadMethod() != null ) {
//                System.out.println(propName+"    "+propertyDescriptor.getReadMethod().getReturnType().getName());
//                if (propertyDescriptor.getReadMethod().getReturnType().getName().equalsIgnoreCase("java.lang.String")&&src.getPropertyValue(propName)==null) {
//                    try {
//                        propertyDescriptor.getWriteMethod().invoke(data,"");
//                    } catch (IllegalAccessException e) {
//                        log.info("反射错误");
//                        e.printStackTrace();
//                    } catch (InvocationTargetException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        }
        this.data = data;
    }
}
